﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace CoreUtils.Common
{
    /// <summary>
    /// Check dependency programs/packages
    /// </summary>
    public class Environment
    {
        /// <summary>
        /// Check version of .Net FrameWork
        /// </summary>
        /// <param name="version">.Net Framework version</param>
        /// <returns></returns>
        /// <remarks>xula@vnext.vn</remarks>
        public static bool isGoodDotNetVersion(string version)
        {
            RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(Constants.DOTNET_REGISTRY_KEY);
            string[] version_names = installed_versions.GetSubKeyNames();
            for (int i = 1; i <= version_names.Length - 1; i++)
            {
                if (Constants.DOTNET_VERSION_REQUIRED == version_names[i].ToString())
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Check installed program on Windows
        /// </summary>
        /// <param name="version"></param>
        /// <param name="registry_key"></param>
        /// <returns></returns>
        /// <remarks>xula@vnext.vn</remarks>
        public static bool isInstalledOnWindows(string registry_key)
        {
            using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
            {
                foreach (string subkey_name in key.GetSubKeyNames())
                {
                    using (RegistryKey subkey = key.OpenSubKey(subkey_name))
                    {
                        var name = (string)subkey.GetValue("DisplayName");
                        if (!string.IsNullOrEmpty(name))
                        {
                            if (Constants.WIL_VERSION_REQUIRED.Substring(0, 3) == name.Substring(0, 3))
                            {
                                // version required
                                double versionProgram = Convert.ToDouble(Constants.WIL_VERSION_REQUIRED.Substring(4, 3));
                                // version of environnement
                                double versionReq = Convert.ToDouble(name.Substring(4, 3));
                                if (versionProgram >= versionReq)
                                    return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Check version of WIL Library
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        /// <remarks>xula@vnext.vn</remarks>
        public static bool isGoodWILVersion()
        {
            return isInstalledOnWindows(Constants.WIL_REGISTRY_KEY_32bit)
                || isInstalledOnWindows(Constants.WIL_REGISTRY_KEY_64bit);
        }
    }
}
